﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using ViewMaker.Core.Controls;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Data;
using ViewMaker.Core.Utils;

namespace ViewMaker.Core.Wpf
{
    public class WpfWindowView : IView
    {

        public Window Window
        {
            get { return _window; }
        }
        private Window _window;

        private ViewLayoutItem rootItem;

        public WpfWindowView(ViewLayoutItem rootItem, object viewModel)
        {
            this.rootItem = rootItem;
            this._window = new Window();
            _window.DataContext = viewModel;
            ViewCommandBehavior.SetHelper(this._window, ServiceLocator.GetService<IViewCommandHelper>());
            SetWindowsInfo();
        }

        private void SetWindowsInfo()
        {
            if (rootItem.Caption != null) this.Window.Title = rootItem.Caption;
            var winSettigs = rootItem.Element.WindowSettings;

            if (winSettigs.State.HasValue)
            {
                Window.WindowState = winSettigs.State.Value;
            }
            else
            {
                this.Window.SizeToContent = SizeToContent.WidthAndHeight;
                this.Window.ContentRendered += delegate { this.Window.SizeToContent = SizeToContent.Manual; };
                if (winSettigs.Height.HasValue)
                {
                    Window.Height = winSettigs.Height.Value;
                    this.Window.SizeToContent = SizeToContent.Width;
                }

                if (winSettigs.Width.HasValue)
                {
                    Window.Width = winSettigs.Width.Value;
                    this.Window.SizeToContent = this.Window.SizeToContent == SizeToContent.Width ? SizeToContent.Manual : SizeToContent.Height;
                }
            }

            if (winSettigs.FontSize.HasValue) Window.FontSize = winSettigs.FontSize.Value;
            if (!string.IsNullOrEmpty(winSettigs.Foreground)) Window.Foreground = (Brush)TypeDescriptor.GetConverter(typeof(Brush)).ConvertFromString(winSettigs.Foreground);
            if (!string.IsNullOrEmpty(winSettigs.Background)) Window.Background = (Brush)TypeDescriptor.GetConverter(typeof(Brush)).ConvertFromString(winSettigs.Background);
            if (winSettigs.ResizeMode.HasValue) Window.ResizeMode = winSettigs.ResizeMode.Value;
            if (!string.IsNullOrEmpty(winSettigs.ClosingCommand)) BindingOperations.SetBinding(Window, WindowClosingBehavior.CommandProperty, new Binding(winSettigs.ClosingCommand));
        }

        #region IView メンバー


        public void Show()
        {
            Window.Show();
        }

        public void ShowDialog()
        {
            Window.ShowDialog();
        }

        public void Close()
        {
            Window.Close();
        }

        public object Content
        {
            get { return Window; }
        }

        #endregion

    }

}
